<?php

/**
 * @author      Nguyen Duc Thuan <me@ndthuan.com>
 * @copyright   Copyright (c) 2009 Nguyen Duc Thuan <me@ndthuan.com>
 * @version     $Id: ConfigVo.php 12 2009-11-03 03:17:46Z thuan.nguyen $
 */

class ConfigVo extends Fete_Model_DataRow
{
    /**
     * @var ConfigTable
     */
    protected $_dbTable = null;

    protected $_configValues = array();

    /**
     *
     * @param mixed $value
     * @return ConfigVo
     */
    public function &setCfKey($value)
    {
        $this->_values['cf_key'] = $value;
        return $this;
    }

    /**
     * @return mixed
     */
    public function getCfKey()
    {
        return isset($this->_values['cf_key'])
            ? $this->_values['cf_key']
            : null;
    }    /**
     *
     * @param mixed $value
     * @return ConfigVo
     */
    public function &setCfValue($value)
    {
        $this->_values['cf_value'] = $value;
        return $this;
    }

    /**
     * @return mixed
     */
    public function getCfValue()
    {
        return isset($this->_values['cf_value'])
            ? $this->_values['cf_value']
            : null;
    }    /**
     *
     * @param mixed $value
     * @return ConfigVo
     */
    public function &setCfNote($value)
    {
        $this->_values['cf_note'] = $value;
        return $this;
    }

    /**
     * @return mixed
     */
    public function getCfNote()
    {
        return isset($this->_values['cf_note'])
            ? $this->_values['cf_note']
            : null;
    }

    /**
     *
     * @return ConfigVo
     */
    public function &save()
    {
        if (null !== $this->_values['cf_key']) {
            $id = $this->_values['cf_key'];
            unset($this->_values['cf_key']);
            $this->getDbTable()->update($this->_values,
                "cf_key = '" . mysql_escape_string($id) . "'");
        } else {
            $this->getDbTable()->insert($this->_values);
        }

        return $this;
    }

    /**
     * @param ConfigTable $table
     * @return ConfigVo
     */
    public function &setDbTable(ConfigTable $table)
    {
        $this->_dbTable = $table;
        return $this;
    }

    /**
     *
     * @return ConfigTable
     */
    public function &getDbTable()
    {
        if (null === $this->_dbTable) {
            $this->_dbTable = new ConfigTable();
        }

        return $this->_dbTable;
    }

    /**
     *
     * @return ConfigVo
     */
    public function &loadAll()
    {
        $ds = $this->_dbTable->fetch('', new Fete_Model_DataSet());

        while ($ds->read())
        {
            $row = $ds->current();
            $this->_configValues[$row->get('cf_key')] = $row->get('cf_value');
        }

        return $this;
    }

    /**
     *
     * @return ConfigVo
     */
    public function &saveValues()
    {
        $values = array();
        foreach ($this->_configValues as $key => $value)
        {
            $values[] = "('" . mysql_escape_string($key) . "', '"
            . $value . "')";
        }
        $this->getDbTable()->getDb()->execute(
            "REPLACE INTO config (cf_key, cf_value) VALUES "
            . implode(', ', $values));
        return $this;
    }

    public function getValue($key)
    {
        return isset($this->_values[$key]) ? $this->_values[$key] : null;
    }

    /**
     *
     * @param mixed $var
     * @return ConfigVo
     */
    static public function genInstance()
    {
        return new self();
    }
}